package com.mcxx.modules.miniSalvation.family.service.read;

import cn.hutool.core.date.DateField;
import com.mcxx.common.exception.BizException;
import com.mcxx.constant.DictConstant;
import com.mcxx.modules.base.service.read.DictionaryReadService;
import com.mcxx.modules.miniSalvation.check.dto.CheckMemberDTO;
import com.mcxx.modules.miniSalvation.check.dto.IntegrateCheckMemberDTO;
import com.mcxx.modules.miniSalvation.family.dao.read.FamilyMemberHistoryReadDao;
import com.mcxx.modules.miniSalvation.family.dao.read.FamilyMemberReadDao;
import com.mcxx.modules.miniSalvation.family.dao.read.FamilyThreeMemberDao;
import com.mcxx.modules.miniSalvation.family.dto.*;
import com.mcxx.modules.miniSalvation.family.entity.FamilyMemberEntity;
import com.mcxx.modules.miniSalvation.family.service.write.FamilyMainWriteService;
import com.mcxx.modules.miniSalvation.family.service.write.FamilyMemberWriteService;
import com.mcxx.modules.miniSalvation.order.service.read.BlacklistReadService;
import com.mcxx.modules.miniSalvation.order.service.read.OrderReadService;
import com.mcxx.modules.miniSalvation.salary.service.read.FamilyMemberSalaryReadService;
import com.mcxx.result.BaseResult;
import com.mcxx.util.*;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import com.yinhai.ta404.module.dict.entity.TaDictPo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;



@Service("familyMemberReadService")
@TaTransactional(readOnly = true)
public class FamilyMemberReadService {
    @Autowired
    private FamilyMemberReadDao familyMemberReadDao;
    @Autowired
    private FamilyMemberHistoryReadDao familyMemberHistoryReadDao;
    @Autowired
    private FamilySupportReadService familySupportReadService;
    @Autowired
    private DictionaryReadService dictionaryReadService;
    @Autowired
    private BlacklistReadService blacklistReadService;
    @Autowired
    private FamilyThreeMemberDao familyThreeMemberDao;
    @Autowired
    private OrderReadService orderReadService;
    @Autowired
    private FamilyMainReadService familyMainReadService;
    @Autowired
    private FamilyMainWriteService familyMainWriteService;
    @Autowired
    private FamilyMemberWriteService familyMemberWriteService;
    @Autowired
    private FamilyMemberSalaryReadService memberSalaryReadService;

    
    public List<FamilyMemberEntity> listAll() {
        return familyMemberReadDao.listAll();
    }

    
    public FamilyMemberEntity getById(String id) {
        return familyMemberReadDao.getById(id);
    }

    
    public FamilyMemberDTO getByIdAndBusinessCode(String id,String businessCode) {
        return familyMemberReadDao.getByIdAndBusinessCode(id,businessCode);
    }

    
    public FamilyMemberDTO getFamilyMemberListForMoveOut(String memberId) {

        FamilyMemberDTO familyMemberEntity = familyMemberHistoryReadDao.getMemberByMemberId(memberId);
        if(null == familyMemberEntity){
            familyMemberEntity = familyMemberHistoryReadDao.getMemberLowByMemberId(memberId);
        }

//        familyMemberEntity.setFamilyRelation(Constant.DomesticRelation.ONESELF);
        familyMemberEntity.setState(Constant.PeopleState.NORMAL);
        return familyMemberEntity;
    }

    public FamilyMemberEntity getFamilyMasterMemberByOrderId(String orderId) {
        return familyMemberReadDao.queryFamilyMasterByOrderId(orderId);
    }

    
    public FamilyMemberEntity getMemberStateByCardId(String idcard) {
        return familyMemberReadDao.queryMemberStateByCardId(idcard);
    }

    
    public BaseResult<MemberCardRtnDTO> checkCardIdExist(String idcard) {
        BaseResult<MemberCardRtnDTO> rstData = this.checkCardIdExistFull(idcard);
        this.checkCardIdExistSingle(idcard, rstData);
        return rstData;
    }

    
    public BaseResult<MemberCardRtnDTO> checkCardIdExistFull(String idcard) {
        BaseResult<MemberCardRtnDTO> rstData = new BaseResult<>();

        if (CommonUtil.invalidArgs(idcard) || !IdCardUtil.identityCardVerification(idcard).getData()) {
            throw new BizException(FailureCode.ERR_10030);
        }

        int rst = blacklistReadService.cntByIdcard(idcard);
        if(rst > 0){
            throw new BizException(FailureCode.ERR_10033);
        }


        MemberCardRtnDTO memberCardRtn = familyMemberHistoryReadDao.queryMemberHistoryByCardId(idcard);
        if(null != memberCardRtn){

            this.dealFinishedOrder(memberCardRtn, rstData);
        }


        memberCardRtn = familyMemberReadDao.queryMemberByCardId(idcard);
        if(null != memberCardRtn){

            this.dealUnfinishedOrder(memberCardRtn, rstData);
            return rstData;
        }
        return rstData;
    }

    
    public void checkCardIdExistSingle(String idcard, BaseResult<MemberCardRtnDTO> rstData) {

        MemberCardRtnDTO memberCardRtn = familyMemberReadDao.queryLowerMemberByCardId(idcard);
        if(null != memberCardRtn){

            throw new BizException("-1", "该成员在低保家庭认定业务办理中");
        }


        memberCardRtn = familyMemberHistoryReadDao.queryLowerMemberHistoryByCardId(idcard);
        if(null != memberCardRtn){
            int countNum = familyMainReadService.getByMemberIdCard(idcard);
            if(countNum<1){

                familyMainWriteService.copyFamilyMainSource(memberCardRtn.getFamilyId());
                familyMemberWriteService.copyFamilyMemberSource(memberCardRtn.getFamilyId());
            }

            memberCardRtn.setFamilyObjectType(Constant.MinimumLivingType.SINGLE);
            rstData.setData(memberCardRtn);
        }
        else{
            memberCardRtn = new MemberCardRtnDTO();
            memberCardRtn.setFamilyObjectType(Constant.MinimumLivingType.HOUSEHOLD);
            rstData.setData(memberCardRtn);
        }
    }

    
    private String packagingCardMsgNoSalvation(MemberCardRtnDTO memberCardRtn){

        TaDictPo businessTypeDict = dictionaryReadService.getCodeCache(DictConstant.BUSINESS_TYPE, memberCardRtn.getBusinessType());

        TaDictPo orderTypeDict = dictionaryReadService.getCodeCache(DictConstant.OPERATION_TYPE, memberCardRtn.getOrderType());

        TaDictPo orderBizStateDict = dictionaryReadService.getCodeCache(DictConstant.BIZSTATE, memberCardRtn.getOrderState());

        TaDictPo memberStateDict = dictionaryReadService.getCodeCache(DictConstant.SURVIVAL_STATUS, memberCardRtn.getMemberState());

        StringBuffer msg = new StringBuffer();
        msg.append("该人员在 ")
                .append(memberCardRtn.getCountyCode())
                .append(memberCardRtn.getStreetCode())
                .append(memberCardRtn.getAreaCode())
                .append("【")
                .append(memberCardRtn.getMasterMemberName())
                .append("】家庭中办理 ")
                .append(businessTypeDict.getLabel())
                .append(orderTypeDict.getLabel())
                .append(",")
                .append(orderBizStateDict.getLabel())
                .append(",该人员状态:");

        if(Constant.SuspendFlag.SUSPEND_STATE.equals(memberCardRtn.getSuspendFlag())){
            msg.append("暂停");
        }else{
            msg.append(memberStateDict.getLabel());
        }


        if(Constant.PeopleState.NORMAL.equals(memberCardRtn.getMemberState())
                && !Constant.SuspendFlag.SUSPEND_STATE.equals(memberCardRtn.getSuspendFlag())
                && Constant.BizState.APPROVE_YES.equals(memberCardRtn.getOrderState())){
            msg.append(",本月仍在救助");
        }
        return msg.toString();
    }

    
    private String packagingCardMsgCanSavation(MemberCardRtnDTO memberCardRtn){

        TaDictPo businessTypeDict = dictionaryReadService.getCodeCache(DictConstant.BUSINESS_TYPE, memberCardRtn.getBusinessType());

        StringBuffer msg = new StringBuffer();
        msg.append("该人员在 ")
                .append(memberCardRtn.getCountyCode())
                .append(memberCardRtn.getStreetCode())
                .append(memberCardRtn.getAreaCode())
                .append("【")
                .append(memberCardRtn.getMasterMemberName())
                .append("】家庭 ")
                .append(businessTypeDict.getLabel())
                .append(",保障")
                .append(memberCardRtn.getEndMonth())
                .append("时间停保。");
        return msg.toString();
    }

    
    private void dealUnfinishedOrder(MemberCardRtnDTO memberCardRtn, BaseResult<MemberCardRtnDTO> rstData){

        if(Constant.BizStateNew.AUDIT_REFUSE.equals(memberCardRtn.getOrderState())
                || Constant.BizStateNew.APPROVE_REFUSE.equals(memberCardRtn.getOrderState())
                || Constant.BizStateNew.TOWN_DELETE.equals(memberCardRtn.getOrderState())){
            String msg = this.packagingCardMsgNoSalvation(memberCardRtn);
//            if(Constant.DomesticRelation.ONESELF.equals(memberCardRtn.getFamilyRelation())){

                msg += "确认录入吗？";
//                rstData.setCode(FailureCode.ERR_10034.getCode());
                rstData.setMsg(msg);
                rstData.setData(memberCardRtn);



//                throw new BizException(FailureCode.ERR_10035.getCode(), msg);

        }else{

            String msg = packagingCardMsgNoSalvation(memberCardRtn);
            msg += "业务办理中";
            throw new BizException(FailureCode.ERR_10036.getCode(), msg);
        }
    }

    
    private void dealFinishedOrder(MemberCardRtnDTO memberCardRtn, BaseResult<MemberCardRtnDTO> rstData){

        String memberState = memberCardRtn.getMemberState();
        String orderType = memberCardRtn.getOrderType();
        String orderState = memberCardRtn.getOrderState();
        if(null == memberState || null == orderType){
            throw new BizException(FailureCode.ERR_10047);
        }

        if(Constant.BizStateNew.TOWN_DELETE.equals(orderState) || Constant.BizStateNew.APPROVE_REFUSE.equals(orderState)){
            String msg = packagingCardMsgNoSalvation(memberCardRtn);
            msg += "确认录入吗？";
            rstData.setCode(FailureCode.ERR_10034.getCode());
            rstData.setMsg(msg);
            rstData.setData(memberCardRtn);
            return;
        }
        

        SimpleDateFormat sdf  =new SimpleDateFormat("yyyyMM");
        boolean rst = false;
        try {
            if(StringUtils.isEmpty(memberCardRtn.getEndMonth())){
                rst = true;
            }else{
                Date dateD = sdf.parse(memberCardRtn.getEndMonth());
                String todayD = sdf.format(new Date());
                Date todayF= sdf.parse(todayD);
                rst = dateD.getTime() < todayF.getTime();
            }


        } catch (ParseException e1) {
            e1.printStackTrace();
        }


        if(Constant.PeopleState.DEAD.equals(memberState) || (Constant.PeopleState.NORMAL.equals(memberState)) ||
                (Constant.SuspendFlag.SUSPEND_STATE.equals(memberCardRtn.getSuspendFlag())
                        && !Constant.OrderType.EXPIRE.equals(orderType))){
            if(Constant.PeopleState.NORMAL.equals(memberState)){
                if(!rst){
                    throw new BizException(FailureCode.ERR_10037.getCode(), packagingCardMsgNoSalvation(memberCardRtn));
                }
            }else{
                throw new BizException(FailureCode.ERR_10037.getCode(), packagingCardMsgNoSalvation(memberCardRtn));
            }

        }

        if(!Constant.OrderType.EXPIRE.equals(orderType) && !Constant.PeopleState.MOVE_OUT_HOUSEHOLD.equals(memberState)
                && !Constant.PeopleState.MARRIED.equals(memberState)){

            int isHouseHoldCancel =  familyMemberReadDao.selectHouseholdSuspendByHistory(memberCardRtn.getOrderId());
            if(isHouseHoldCancel>0){
                if(!Constant.PeopleState.DEAD.equals(memberState)){
                    String msg = packagingCardMsgNoSalvation(memberCardRtn);
                    msg += "确认录入吗？";
                    rstData.setCode(FailureCode.ERR_10034.getCode());
                    rstData.setMsg(msg);
                    rstData.setData(memberCardRtn);
                    return;
                }
            }
            String msg = packagingCardMsgNoSalvation(memberCardRtn);
            msg += "，如需继续救助，可去原家庭恢复";
            if(!Constant.PeopleState.NORMAL.equals(memberState)){
                throw new BizException(FailureCode.ERR_10035.getCode(), msg);
            }

        }

        if(Constant.OrderType.EXPIRE.equals(orderType) && Constant.PeopleState.EXPIRE.equals(memberState)){
            String msg = this.packagingCardMsgCanSavation(memberCardRtn);
            msg += "确认录入吗？";
            rstData.setCode(FailureCode.ERR_10034.getCode());
            rstData.setMsg(msg);
        }else if(Constant.PeopleState.MOVE_OUT_HOUSEHOLD.equals(memberState)){
            String msg = this.packagingCardMsgCanSavation(memberCardRtn);
            msg += "原因是：分户迁出,确定要重新录入？";

            rstData.setCode(FailureCode.ERR_10048.getCode());
            rstData.setMsg(msg);
        }else if(Constant.PeopleState.MARRIED.equals(memberState)){
            String msg = this.packagingCardMsgCanSavation(memberCardRtn);
            msg += "原因是：嫁娶,确定要重新录入？";

            rstData.setCode(FailureCode.ERR_10048.getCode());
            rstData.setMsg(msg);
        }
        if((Constant.PeopleState.NORMAL.equals(memberState)) && rst){
            String msg = this.packagingCardMsgCanSavation(memberCardRtn);
            msg += "确认录入吗？";
            rstData.setCode(FailureCode.ERR_10034.getCode());
            rstData.setMsg(msg);
        }
        rstData.setData(memberCardRtn);
    }
    
    public BaseResult<FamilyThreeMemberDTO> checkIdcardForAddMember(String idcard, String familyId){
        BaseResult<FamilyThreeMemberDTO> baseRst = new BaseResult<>();
        baseRst =  checkIdcardForAddMemberMin(idcard,familyId);
        if("0".equals(baseRst.getCode())){
            baseRst =  checkIdcardForAddMemberLow(idcard,familyId);
        }
        return baseRst;

    }

    
    public BaseResult<FamilyThreeMemberDTO> checkIdcardForAddMemberMin(String idcard, String familyId){
        BaseResult<FamilyThreeMemberDTO> baseRst = new BaseResult<>();

        if (CommonUtil.invalidArgs(idcard) || !IdCardUtil.identityCardVerification(idcard).getData()) {
            throw new BizException(FailureCode.ERR_10030);
        }

        int rst = blacklistReadService.cntByIdcard(idcard);
        if(rst > 0){
            throw new BizException(FailureCode.ERR_10033);
        }


        rst = familySupportReadService.cntInSupport(familyId, idcard);
        if(rst > 0){
            throw new BizException(FailureCode.ERR_10050);
        }


        FamilyThreeMemberDTO familyThreeMemberDTO  = familyThreeMemberDao.queryMemberByCardId(idcard);
        FamilyThreeMemberDTO familyThreeMembersDTO = new FamilyThreeMemberDTO();
        if(!CommonUtil.invalidArgs(familyThreeMemberDTO)){
            familyThreeMembersDTO = familyThreeMemberDTO;
            baseRst.setData(familyThreeMemberDTO);
        }


        MemberCardRtnDTO memberCardRtn = familyMemberHistoryReadDao.getMemberStateByIdcard(idcard);

        if(null == memberCardRtn){
            return baseRst;
        }

        memberSalaryReadService.checkMemberIsSaved(memberCardRtn.getMemberId(), DateUtil.getMonthString());



        String tempName = CommonUtil.invalidArgs(memberCardRtn.getMasterMemberName()) ? memberCardRtn.getApplyMemberName() : memberCardRtn.getMasterMemberName();

        if(Constant.BizStateNew.TOWN_DELETE.equals(memberCardRtn.getOrderState())){
            baseRst.setCode(FailureCode.ERR_10035.getCode());
            familyThreeMembersDTO.setMemberId(memberCardRtn.getMemberId());
            baseRst.setData(familyThreeMembersDTO);
            baseRst.setMsg("该成员在曾经在家庭【" + tempName + "】录入，现乡镇已删除，是否确认要重新加入当前家庭享受");
            return baseRst;
        }

        if(Constant.BizStateNew.APPROVE_REFUSE.equals(memberCardRtn.getOrderState())){
            baseRst.setCode(FailureCode.ERR_10035.getCode());
            familyThreeMembersDTO.setMemberId(memberCardRtn.getMemberId());
            baseRst.setData(familyThreeMembersDTO);
            baseRst.setMsg("该成员在曾经在家庭【" + tempName + "】录入，且状态为拒批，是否确认要重新加入当前家庭享受");
            return baseRst;
        }


        if(Constant.PeopleState.NORMAL.equals(memberCardRtn.getMemberState())){
            throw new BizException(FailureCode.ERR_10036);
        }

        if(Constant.PeopleState.DEAD.equals(memberCardRtn.getMemberState())){
            throw new BizException(FailureCode.ERR_10037);
        }

        if(Constant.OrderType.SUSPEND.equals(memberCardRtn.getOrderType())){
            throw new BizException(FailureCode.ERR_10036);
        }

        if(Constant.PeopleState.MISSING.equals(memberCardRtn.getMemberState())){
            throw new BizException(FailureCode.ERR_10035.getCode(), "身份证在家庭【" + tempName + "】中曾参保，现状态为：失踪，如果需继续在享需前原家庭恢复");
        }

        if(Constant.PeopleState.IN_PRISON.equals(memberCardRtn.getMemberState())){
            throw new BizException(FailureCode.ERR_10035.getCode(), "身份证在家庭【" + tempName + "】中曾参保，现状态为：狱中服刑，如果需继续在享需前原家庭恢复");
        }

        if(Constant.PeopleState.EXPIRE.equals(memberCardRtn.getMemberState())){

//            int nums = familyMemberReadDao.getMemberNumberByFmyId(memberCardRtn.getFamilyId());
            int nums = familyMemberHistoryReadDao.getMemberNumberByFmyIdAndOrderId(memberCardRtn.getFamilyId(),memberCardRtn.getOrderId());
            if(nums > 0){
                throw new BizException(FailureCode.ERR_10035.getCode(), "身份证在家庭【" + tempName + "】中曾参保，现状态为：停保，如果需继续享受保障需前原家庭恢复");
            }else{
                baseRst.setCode(FailureCode.ERR_10035.getCode());
                familyThreeMembersDTO.setMemberId(memberCardRtn.getMemberId());
                baseRst.setData(familyThreeMembersDTO);
                baseRst.setMsg("该成员在曾经在家庭【" + tempName + "】享受过待遇，现已停保，是否确认要重新加入当前家庭享受");
                return baseRst;
            }


        }

        if(Constant.PeopleState.SUSPEND_STATE.equals(memberCardRtn.getMemberState())){
            throw new BizException(FailureCode.ERR_10035.getCode(), "身份证在家庭【" + tempName + "】中曾参保，现状态为：暂停，如果需继续享受保障需前原家庭恢复");
        }

        if(Constant.PeopleState.MARRIED.equals(memberCardRtn.getMemberState())){
            baseRst.setCode(FailureCode.ERR_10035.getCode());
            familyThreeMembersDTO.setMemberId(memberCardRtn.getMemberId());
            baseRst.setData(familyThreeMembersDTO);
            baseRst.setMsg("该成员在曾经在家庭【" + tempName + "】享受过待遇，现已嫁娶，是否确认要重新加入当前家庭享受");
            return baseRst;}

        if(Constant.PeopleState.OTHER.equals(memberCardRtn.getMemberState())){
            throw new BizException(FailureCode.ERR_10035.getCode(), "身份证在家庭【" + tempName + "】中曾参保，现状态为：其他，如果需继续享受保障需前原家庭恢复");
        }
        baseRst.setCode(FailureCode.ERR_10035.getCode());
        familyThreeMembersDTO.setMemberId(memberCardRtn.getMemberId());
        baseRst.setData(familyThreeMembersDTO);
        baseRst.setMsg("该成员在曾经在家庭【" + tempName + "】享受过待遇，现已分户迁出，是否确认要重新加入当前家庭享受");
        return baseRst;
    }

    
    public String checkIdcardForRevoke(String idcard){
        BaseResult<FamilyThreeMemberDTO> baseRst = new BaseResult<>();

        if (CommonUtil.invalidArgs(idcard) || !IdCardUtil.identityCardVerification(idcard).getData()) {
            throw new BizException("-1","成员【"+idcard+"】无效，该业务不可撤销");
        }

        int rst = blacklistReadService.cntByIdcard(idcard);
        if(rst > 0){
            throw new BizException("-1","成员【"+idcard+"】已经加入黑名单，该业务不可撤销！");
        }

        FamilyThreeMemberDTO familyThreeMemberDTO  = familyThreeMemberDao.queryMemberByCardId(idcard);
        FamilyThreeMemberDTO familyThreeMembersDTO = new FamilyThreeMemberDTO();
        if(!CommonUtil.invalidArgs(familyThreeMemberDTO)){
            familyThreeMembersDTO = familyThreeMemberDTO;
            baseRst.setData(familyThreeMemberDTO);
        }


        MemberCardRtnDTO memberCardRtn = familyMemberHistoryReadDao.getMemberStateByIdcard(idcard);

        if(null == memberCardRtn){
            return null;
        }


        String tempName = CommonUtil.invalidArgs(memberCardRtn.getMasterMemberName()) ? memberCardRtn.getApplyMemberName() : memberCardRtn.getMasterMemberName();

        if(Constant.BizStateNew.TOWN_DELETE.equals(memberCardRtn.getOrderState())){
            throw new BizException("-1","成员【"+idcard+"】办理了新业务，该业务不可撤销");
        }

        if(Constant.BizStateNew.APPROVE_REFUSE.equals(memberCardRtn.getOrderState())
                || Constant.BizStateNew.APPROVE_YES.equals(memberCardRtn.getOrderState())){
            return null;
        } else {
            throw new BizException("-1", "成员【"+idcard+"】办理了新业务，该业务不可撤销！");
        }
    }

    
    public BaseResult<FamilyThreeMemberDTO> checkIdcardForAddMemberLow(String idcard, String familyId){
        BaseResult<FamilyThreeMemberDTO> baseRst = new BaseResult<>();

        if (CommonUtil.invalidArgs(idcard) || !IdCardUtil.identityCardVerification(idcard).getData()) {
            throw new BizException(FailureCode.ERR_10030);
        }

        int rst = blacklistReadService.cntByIdcard(idcard);
        if(rst > 0){
            throw new BizException(FailureCode.ERR_10033);
        }


        rst = familySupportReadService.cntInSupport(familyId, idcard);
        if(rst > 0){
            throw new BizException(FailureCode.ERR_10050);
        }


        FamilyThreeMemberDTO familyThreeMemberDTO  = familyThreeMemberDao.queryMemberByCardId(idcard);
        FamilyThreeMemberDTO familyThreeMembersDTO = new FamilyThreeMemberDTO();
        if(!CommonUtil.invalidArgs(familyThreeMemberDTO)){
            familyThreeMembersDTO = familyThreeMemberDTO;
            baseRst.setData(familyThreeMemberDTO);
        }


        MemberCardRtnDTO memberCardRtn = familyMemberHistoryReadDao.getLowMemberStateByIdcard(idcard);

        if(null == memberCardRtn){
            return baseRst;
        }
        if(familyId.equals(memberCardRtn.getFamilyId())){
            baseRst.setCode(FailureCode.ERR_10035.getCode());
            familyThreeMembersDTO.setMemberId(memberCardRtn.getMemberId());
            baseRst.setData(familyThreeMembersDTO);
            baseRst.setMsg("该成员在曾经在该家庭录入并删除，是否重新录入");
            return baseRst;
        }


        String tempName = CommonUtil.invalidArgs(memberCardRtn.getMasterMemberName()) ? memberCardRtn.getApplyMemberName() : memberCardRtn.getMasterMemberName();

        if(Constant.BizStateNew.TOWN_DELETE.equals(memberCardRtn.getOrderState())){
            baseRst.setCode(FailureCode.ERR_10035.getCode());
            familyThreeMembersDTO.setMemberId(memberCardRtn.getMemberId());
            baseRst.setData(familyThreeMembersDTO);
            baseRst.setMsg("该成员在曾经在低保边缘家庭【" + tempName + "】录入，现乡镇已删除，是否确认要重新加入当前家庭享受");
            return baseRst;
        }

        if(Constant.BizStateNew.APPROVE_REFUSE.equals(memberCardRtn.getOrderState())){
            baseRst.setCode(FailureCode.ERR_10035.getCode());
            familyThreeMembersDTO.setMemberId(memberCardRtn.getMemberId());
            baseRst.setData(familyThreeMembersDTO);
            baseRst.setMsg("该成员在曾经在低保边缘家庭【" + tempName + "】录入，且状态为拒批，是否确认要重新加入当前家庭享受");
            return baseRst;
        }


        if(Constant.PeopleState.NORMAL.equals(memberCardRtn.getMemberState())){
            throw new BizException(FailureCode.ERR_10036);
        }

        if(Constant.PeopleState.DEAD.equals(memberCardRtn.getMemberState())){
            throw new BizException(FailureCode.ERR_10037);
        }

        if(Constant.OrderType.SUSPEND.equals(memberCardRtn.getOrderType())){
            throw new BizException(FailureCode.ERR_10036);
        }

        if(Constant.PeopleState.MISSING.equals(memberCardRtn.getMemberState())){
            throw new BizException(FailureCode.ERR_10035.getCode(), "身份证在低保边缘家庭【" + tempName + "】中曾参保，现状态为：失踪，如果需继续在享需前原家庭恢复");
        }

        if(Constant.PeopleState.IN_PRISON.equals(memberCardRtn.getMemberState())){
            throw new BizException(FailureCode.ERR_10035.getCode(), "身份证在低保边缘家庭【" + tempName + "】中曾参保，现状态为：狱中服刑，如果需继续在享需前原家庭恢复");
        }

        if(Constant.PeopleState.EXPIRE.equals(memberCardRtn.getMemberState())){

            int nums = familyMemberReadDao.getMemberNumberByFmyId(memberCardRtn.getFamilyId());
            if(nums > 0){
                throw new BizException(FailureCode.ERR_10035.getCode(), "身份证在低保边缘家庭【" + tempName + "】中曾参保，现状态为：停保，如果需继续享受保障需前原家庭恢复");
            }else{
                baseRst.setCode(FailureCode.ERR_10035.getCode());
                familyThreeMembersDTO.setMemberId(memberCardRtn.getMemberId());
                baseRst.setData(familyThreeMembersDTO);
                baseRst.setMsg("该成员在曾经在低保边缘家庭【" + tempName + "】享受过待遇，现已停保，是否确认要重新加入当前家庭享受");
                return baseRst;
            }


        }

        if(Constant.PeopleState.SUSPEND_STATE.equals(memberCardRtn.getMemberState())){
            throw new BizException(FailureCode.ERR_10035.getCode(), "身份证在低保边缘家庭【" + tempName + "】中曾参保，现状态为：暂停，如果需继续享受保障需前原家庭恢复");
        }

        if(Constant.PeopleState.MARRIED.equals(memberCardRtn.getMemberState())){
            baseRst.setCode(FailureCode.ERR_10035.getCode());
            familyThreeMembersDTO.setMemberId(memberCardRtn.getMemberId());
            baseRst.setData(familyThreeMembersDTO);
            baseRst.setMsg("该成员在曾经在低保边缘家庭【" + tempName + "】享受过待遇，现已嫁娶，是否确认要重新加入当前家庭享受");
            return baseRst;}

        if(Constant.PeopleState.OTHER.equals(memberCardRtn.getMemberState())){
            throw new BizException(FailureCode.ERR_10035.getCode(), "身份证在低保边缘家庭【" + tempName + "】中曾参保，现状态为：其他，如果需继续享受保障需前原家庭恢复");
        }
        baseRst.setCode(FailureCode.ERR_10035.getCode());
        familyThreeMembersDTO.setMemberId(memberCardRtn.getMemberId());
        baseRst.setData(familyThreeMembersDTO);
        baseRst.setMsg("该成员在曾经在低保边缘家庭【" + tempName + "】享受过待遇，现已分户迁出，是否确认要重新加入当前家庭享受");
        return baseRst;
    }

    public Map<String, String> getMemberIdAndFamilyIdByIdCard(String idcard) {
        return familyMemberReadDao.getMemberIdAndFamilyIdByIdCard(idcard);
    }

    public List<FamilyMemberEntity> queryHistoryInfo(String id) {
        return  familyMemberReadDao.queryHistoryInfo(id);
    }

    public List<FamilyMemberEntity> getFamilyRelationMap(String familyId) {
        return familyMemberReadDao.getFamilyRelationMapByFamilyId(familyId);
    }

    
    public List<FamilyMemberEntity> listNormalMemberByFamilyId(String familyId) {
        List<FamilyMemberEntity> familyMemberList = familyMemberReadDao.listNormalMemberByFamilyId(familyId);
        return familyMemberList;
    }

    
    public List<FamilyMemberEntity> listByFamilyId(String familyId) {
        return familyMemberReadDao.listByFamilyId(familyId);
    }

    
    public List<FamilyMemberEntity> listHistroyByOrderId(String orderId) {
        return familyMemberReadDao.listHistroyByOrderId(orderId);
    }

    
    public List<FamilyMemberDTO> listTempByOrderId(String orderId) {
        return familyMemberReadDao.listTempByOrderId(orderId);
    }


    
    public List<FamilyMemberDTO> listMemberByOrderId(String orderId) {
        return familyMemberReadDao.listMemberByOrderId(orderId);
    }

    
    public List<String> getMasterInfoNoPerfect(List<String> idcardList){
        return  familyMemberReadDao.getMasterInfoNoPerfect(idcardList);
    }

    
    public List<CheckMemberDTO> selectMemberListInfo(String familyId) { return familyMemberReadDao.selectMemberListInfo(familyId);}

    
    public List<FamilyMemberEntity> getFirstStartDate(String familyId){
        return familyMemberReadDao.getFirstStartDate(familyId);
    }
    
    public  List<String> selectMasterAndSave(List<String> idList){
        return familyMemberReadDao.selectMasterCount(idList);
    }

    
    public List<MemberSalaryOrderDTO> getMemberInfoByOrderId(String orderId){
        return familyMemberReadDao.getMemberInfoByOrderId(orderId);
    }

    
    public int cntFamilyMaster(String familyId){
        return familyMemberReadDao.cntFamilyMaster(familyId);
    }

    
    public List<String> cntFamilyMasterErrorList(List<String> familyIdList){
        return familyMemberReadDao.cntFamilyMasterErrorList(familyIdList);
    }

    
    public String cntCompleteInfoNum(String familyId, String businessType,String standType){

        if(Constant.BusinessCode.DIBAO.equals(businessType)&&CommonUtil.invalidArgs(standType)){
            standType = orderReadService.selectStandByFamilyId(familyId);
        }
        return familyMemberReadDao.cntCompleteInfoNum(familyId, businessType,standType);
    }

    
    public String cntCompleteInfoNum(String familyId, String businessType){
        return familyMemberReadDao.cntCompleteInfoNum(familyId, businessType,null);
    }

    
    public List<String> cntCompleteInfoNumList(List<String> familyIdList){
        return familyMemberReadDao.cntCompleteInfoNumList(familyIdList);
    }

    
    public List<FamilyMemberEntity> getExMemberInfoByOrderId(String orderId){ return familyMemberReadDao.getExMemberInfoByOrderId(orderId);};

    
    public String getMasterIsHelpPoor(String familyId){
        return familyMemberReadDao.getMasterIsHelpPoor(familyId);
    }
    
    public FamilyMemberEntity getMasertInfo(String familyId){return familyMemberReadDao.getMasertInfo(familyId);}

    public MemberCardRtnDTO queryMemberByCardId(String idcard) {
        return familyMemberReadDao.queryMemberByCardId(idcard);
    }
    
    public List<FamilyMemberEntity> getFamilyApplyMapByFamilyId(String familyId){return familyMemberReadDao.getFamilyApplyMapByFamilyId(familyId);}
    public String getMasterIdCardByFamilyId(String familyId){return familyMemberReadDao.getMasterIdCardByFamilyId(familyId);}


    
    public List<FamilyMemberEntity> getMasterByFamilyIds(List<String> familyIdList){
        return familyMemberReadDao.getMasterByFamilyIds(familyIdList);
    }
    
    public int selectCountByIdcard(String idcard,String familyId){
        return familyMemberReadDao.selectCountByIdcard(idcard,familyId);
    }
    
    public FamilyMemberEntity selectApplyByFamilyId(String familyId){
        return familyMemberReadDao.selectApplyByFamilyId(familyId);
    }
    
    public List<Map<String,Object>> queryListToCalculate(String familyId){
        return familyMemberReadDao.queryListToCalculate(familyId);
    }
    
    public List<Map<String,Object>> queryLowerListToCalculate(String familyId){
        return familyMemberReadDao.queryLowerListToCalculate(familyId);
    }
    
    public int isHouseholdCancel(String orderId){
      return familyMemberReadDao.selectHouseholdSuspend(orderId);
    }

    
    public int selectHouseholdSuspendByFamilyId(String familyId){
        return familyMemberReadDao.selectHouseholdSuspendByFamilyId(familyId);
    }

    
    public MemberCardRtnDTO queryMemberByLowIncomCardId(String idcard){
        return familyMemberReadDao.queryMemberByLowIncomCardId(idcard);
    }
    
    public MemberCardRtnDTO queryInvalidOrderMemberByCardId(String idcard){
        return familyMemberReadDao.queryInvalidOrderMemberByCardId(idcard);
    }
    
    public FamilyMainDTO selectFamilySaveNum(String idcard){
        return familyMemberReadDao.selectFamilySaveNum(idcard);
    }
    
    public List<FamilyMemberEntity> listMemberByIdcard(String idcard){
        return familyMemberReadDao.listMemberByIdcard(idcard);
    }

    
    public MemberCardRtnDTO queryLowerMemberByCardId(String idcard) {
        return familyMemberReadDao.queryLowerMemberByCardId(idcard);
    }

    
    public MemberCardRtnDTO queryLowerDeleteMemberByCardId(String idcard) {
        return familyMemberReadDao.queryLowerDeleteMemberByCardId(idcard);
    }


    
    public int selectHouseholdSuspendByHistory(String orderId) {
        return familyMemberReadDao.selectHouseholdSuspendByHistory(orderId);
    }

    
    public int cntFamilyMember(String familyId){
        return familyMemberReadDao.cntFamilyMember(familyId);
    }

    
    public List<IntegrateCheckMemberDTO> selectIntegrateMember(String orderId,String familyId){
        return familyMemberReadDao.selectIntegrateMember(orderId,familyId);
    }

    public List<FamilyMemberEntity> listLowerByFamilyId(String familyId) {
        return familyMemberReadDao.listLowerByFamilyId(familyId);
    }

    public List<FamilyMemberEntity> listNormalLowerByFamilyId(String familyId) {
        return familyMemberReadDao.listNormalLowerByFamilyId(familyId);
    }
    
    public List<FamilyMemberEntity> selectMemberInFamily(String familyId){
        return familyMemberReadDao.selectMemberInFamily(familyId);
    }

    
    public List<FamilyMemberEntity> listHppLowerByFamilyId(String familyId) {
        List<FamilyMemberEntity> familyMemberList = familyMemberReadDao.listHppLowerByFamilyId(familyId);
        return familyMemberList;
    }
}
